package com.ytt.core.service.service.authority;

import com.ytt.core.model.authority.AuthorityDto;
import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.service.entity.authority.Authority;
import com.ytt.core.service.exception.NoSuchDataException;
import com.ytt.core.service.mapper.authority.AuthorityMapper;
import com.ytt.core.service.repository.AuthorityRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Stream;

/**
 * AuthorityImpl
 *
 * @author chenwen
 * @date 2019/9/16 17:00
 */
@Slf4j
@Service("authorityService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class AuthorityImpl implements AuthorityService {

    private final AuthorityRepository authorityRepository;
    private final AuthorityMapper authorityMapper;

    @Autowired
    public AuthorityImpl(AuthorityRepository authorityRepository, AuthorityMapper authorityMapper) {
        this.authorityRepository = authorityRepository;
        this.authorityMapper = authorityMapper;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuthorityDto findById(String id) {
        Optional<Authority> u = authorityRepository.findById(Long.valueOf(id));
        if (u.isPresent()) {
            AuthorityDto authorityDTO = authorityMapper.toDTO(u.get());
            return authorityDTO;
        } else {
            throw new NoSuchDataException(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageDataDTO<AuthorityDto> findOnePage(Integer page, Integer size, String sort, String type, String name, String code) {
      

        return null;
    }

    @Override
    public List<AuthorityDto> findAll() {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean checkAvailability(String code, String id) {

        Long authorityID = authorityRepository.findAuthoritiesByCode(code);
        if (authorityID == null) {
            return true;
        } else {
            return id != null && id.equalsIgnoreCase(String.valueOf(authorityID));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuthorityDto create(AuthorityDto authorityEditInfoDTO) {
        Authority authority = authorityMapper.toEntity(authorityEditInfoDTO);
        authority = authorityRepository.saveAndFlush(authority);
        String[] roleIds = null;
        if (roleIds != null && roleIds.length > 0) {
            long[] ids = Stream.of(roleIds).mapToLong(Long::parseLong).toArray();
            // 分配角色
            Authority finalAuthority = authority;
            authority = authorityRepository.saveAndFlush(authority);
        }

        AuthorityDto authorityDTO = authorityMapper.toDTO(authority);
        return authorityDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuthorityDto update(String id, AuthorityDto authorityEditInfoDTO) {
        Optional<Authority> o = authorityRepository.findById(Long.valueOf(id));
        Authority authority;
        if (o.isPresent()) {
            authority = o.get();
            authorityMapper.updateEntity(authorityEditInfoDTO, authority);
            String[] roleIds = null;
            authority = authorityRepository.saveAndFlush(authority);
            AuthorityDto authorityDTO = authorityMapper.toDTO(authority);
            return authorityDTO;
        } else {
            throw new NoSuchDataException(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        Optional<Authority> old = authorityRepository.findById(Long.valueOf(id));
        if (old.isPresent()) {
            authorityRepository.delete(old.get());
        } else {
            throw new NoSuchDataException(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void grantRoles(String id, String... roles) {
        Optional<Authority> old = authorityRepository.findById(Long.valueOf(id));
        long[] ids = Stream.of(roles).mapToLong(Long::parseLong).toArray();
        if (!old.isPresent()) {
            throw new NoSuchDataException(id);
        } else {
            authorityRepository.save(old.get());
        }


    }

    @Override
    public Set<String> getAuthorityTypes(String type) {
        if (type != null) {
            return authorityRepository.getAuthorityTypes(type);
        } else {
            return authorityRepository.getAuthorityTypes();
        }
    }
}
